package com.liming.user.service.impl;

import com.liming.auth.client.UserAuthClient;
import com.liming.common.constant.SystemConstants;
import com.liming.common.enums.MiniOrderQueryStatusEnum;
import com.liming.common.pojo.dto.coupon.UserHomeCouponPageQuery;
import com.liming.common.pojo.vo.PageVO;
import com.liming.common.pojo.vo.authinfo.mini.MiniUserHomeVO;
import com.liming.common.pojo.vo.coupon.mini.MiniUserHomeCouponVO;
import com.liming.common.pojo.vo.coupon.openfeign.FeignMiniUserHomeCouponVO;
import com.liming.common.util.PageUtils;
import com.liming.common.util.ThreadLocalUtils;
import com.liming.coupon.client.CouponUserClient;
import com.liming.order.client.UserOrderClient;
import com.liming.user.service.UserInfoService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;

/**
 * @author liming
 * @date 2025/3/29
 */
@Service
@AllArgsConstructor
public class UserInfoServiceImpl implements UserInfoService {

    private final UserAuthClient userAuthClient;
    private final CouponUserClient couponUserClient;
    private final UserOrderClient userOrderClient;

    @Override
    public MiniUserHomeVO miniUserHome() {
        MiniUserHomeVO vo = new MiniUserHomeVO();
        String id = ThreadLocalUtils.get(SystemConstants.ID);
        long userId = Long.parseLong(id);

        CompletableFuture<Map<Long, String>> userIdAndNicknameMapFuture = CompletableFuture.supplyAsync(userAuthClient::userIdsAndNickname);
        CompletableFuture<Map<Long, String>> userIdAndAvatarMapFuture = CompletableFuture.supplyAsync(userAuthClient::userIdsAndAvatar);
        CompletableFuture<Integer> couponCountFuture = CompletableFuture.supplyAsync(() -> couponUserClient.userHaveCoupons(userId).size());
        CompletableFuture<Map<Integer, Integer>> statusAndCountMapFuture = CompletableFuture.supplyAsync(() -> userOrderClient.allStatusCount(userId));
        CompletableFuture.allOf(userIdAndNicknameMapFuture, userIdAndAvatarMapFuture, couponCountFuture, statusAndCountMapFuture).join();

        vo.setNickname(userIdAndNicknameMapFuture.join().get(userId));
        vo.setAvatar(userIdAndAvatarMapFuture.join().get(userId));
        vo.setCouponCount(couponCountFuture.join());
        vo.setPendingPaymentOrderCount(statusAndCountMapFuture.join().getOrDefault(MiniOrderQueryStatusEnum.UNPAID.getCode(), 0));
        vo.setPendingDeliveryOrderCount(statusAndCountMapFuture.join().getOrDefault(MiniOrderQueryStatusEnum.UNSHIPPED.getCode(), 0));
        vo.setPendingReceiptOrderCount(statusAndCountMapFuture.join().getOrDefault(MiniOrderQueryStatusEnum.UNRECEIVED.getCode(), 0));
        vo.setPendingPickupOrderCount(statusAndCountMapFuture.join().getOrDefault(MiniOrderQueryStatusEnum.UNPICKED.getCode(), 0));
        vo.setPendingEvaluationOrderCount(statusAndCountMapFuture.join().getOrDefault(MiniOrderQueryStatusEnum.UNCOMMENTED.getCode(), 0));
        vo.setRefundOrderCount(statusAndCountMapFuture.join().getOrDefault(MiniOrderQueryStatusEnum.REFUND_AFTER_SALE.getCode(), 0));
        return vo;
    }

    @Override
    public PageVO<MiniUserHomeCouponVO> miniUserHomeCoupon(UserHomeCouponPageQuery pageQuery) {
        PageVO<FeignMiniUserHomeCouponVO> vo = couponUserClient.userHomeCouponPage(pageQuery);
        PageVO<MiniUserHomeCouponVO> result = new PageVO<>();
        PageUtils.copyProperties(vo, result, MiniUserHomeCouponVO.class);
        return result;
    }
}
